{% extends "base.html" %}

{% block title %}个人中心 - 万岛潜水{% endblock %}

{% block content %}
<div class="container">
    <div class="row mb-4">
        <div class="col-12">
            <div class="profile-header p-4 rounded shadow-sm">
                <div class="row align-items-center">
                    <div class="col-md-2 text-center">
                        <img src="{{ url_for('static', filename='img/avatar.jpg') }}" class="rounded-circle img-fluid mb-3 mb-md-0" alt="用户头像" style="max-width: 100px;">
                    </div>
                    <div class="col-md-10">
                        <h1 class="h3">{{ current_user.username }}</h1>
                        <p class="text-muted mb-0">{{ current_user.email }}</p>
                        <p class="text-muted mb-0">{{ current_user.phone }}</p>
                        <p class="mb-0">
                            {% if current_user.is_member %}
                            <span class="badge bg-success">会员</span>
                            <small class="text-muted ms-2">会员自：{{ current_user.member_since.strftime('%Y-%m-%d') }}</small>
                            {% else %}
                            <span class="badge bg-secondary">普通用户</span>
                            <a href="#" class="ms-2">升级为会员</a>
                            {% endif %}
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-3 mb-4">
            <div class="list-group shadow-sm">
                <a href="#bookings" class="list-group-item list-group-item-action active" data-bs-toggle="list">我的预约</a>
                <a href="#certificates" class="list-group-item list-group-item-action" data-bs-toggle="list">我的证书</a>
                <a href="#settings" class="list-group-item list-group-item-action" data-bs-toggle="list">账户设置</a>
                <a href="{{ url_for('main.logout') }}" class="list-group-item list-group-item-action text-danger">退出登录</a>
            </div>
        </div>
        
        <div class="col-md-9">
            <div class="tab-content">
                <div class="tab-pane fade show active" id="bookings">
                    <div class="card shadow-sm">
                        <div class="card-header d-flex justify-content-between align-items-center">
                            <h5 class="mb-0">我的预约</h5>
                            <a href="{{ url_for('main.courses') }}" class="btn btn-sm btn-primary">新预约</a>
                        </div>
                        <div class="card-body">
                            {% if bookings %}
                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>课程</th>
                                            <th>日期</th>
                                            <th>教练</th>
                                            <th>人数</th>
                                            <th>金额</th>
                                            <th>状态</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% for booking in bookings %}
                                        <tr>
                                            <td>{{ booking.course.name }}</td>
                                            <td>{{ booking.booking_date.strftime('%Y-%m-%d %H:%M') }}</td>
                                            <td>{{ booking.coach.name }}</td>
                                            <td>{{ booking.participants }}</td>
                                            <td>¥{{ booking.total_price }}</td>
                                            <td>
                                                {% if booking.status == 'pending' %}
                                                <span class="badge bg-warning text-dark">待确认</span>
                                                {% elif booking.status == 'confirmed' %}
                                                <span class="badge bg-success">已确认</span>
                                                {% elif booking.status == 'completed' %}
                                                <span class="badge bg-info">已完成</span>
                                                {% elif booking.status == 'cancelled' %}
                                                <span class="badge bg-danger">已取消</span>
                                                {% endif %}
                                                
                                                {% if booking.payment_status == 'unpaid' %}
                                                <span class="badge bg-danger">未支付</span>
                                                {% else %}
                                                <span class="badge bg-success">已支付</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                {% if booking.payment_status == 'unpaid' %}
                                                <a href="{{ url_for('main.payment', booking_id=booking.id) }}" class="btn btn-sm btn-primary">支付</a>
                                                {% endif %}
                                                
                                                {% if booking.status == 'pending' or booking.status == 'confirmed' %}
                                                <button class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#cancelModal{{ booking.id }}">取消</button>
                                                
                                                <!-- 取消预约确认模态框 -->
                                                <div class="modal fade" id="cancelModal{{ booking.id }}" tabindex="-1" aria-labelledby="cancelModalLabel{{ booking.id }}" aria-hidden="true">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h5 class="modal-title" id="cancelModalLabel{{ booking.id }}">确认取消</h5>
                                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                            </div>
                                                            <div class="modal-body">
                                                                <p>您确定要取消此预约吗？</p>
                                                                <p class="text-danger">注意：活动前24小时内取消将收取50%费用。</p>
                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                                                                <a href="{{ url_for('main.cancel_booking', booking_id=booking.id) }}" class="btn btn-danger">确认取消</a>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                {% endif %}
                                            </td>
                                        </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                            {% else %}
                            <div class="text-center py-5">
                                <i class="fas fa-calendar-times fa-4x text-muted mb-3"></i>
                                <p class="lead">您还没有任何预约</p>
                                <a href="{{ url_for('main.courses') }}" class="btn btn-primary">立即预约</a>
                            </div>
                            {% endif %}
                        </div>
                    </div>
                </div>
                
                <div class="tab-pane fade" id="certificates">
                    <div class="card shadow-sm">
                        <div class="card-header">
                            <h5 class="mb-0">我的证书</h5>
                        </div>
                        <div class="card-body">
                            {% if certificates %}
                            <div class="row">
                                {% for certificate in certificates %}
                                <div class="col-md-6 mb-4">
                                    <div class="card">
                                        <div class="card-body">
                                            <h5 class="card-title">{{ certificate.name }}</h5>
                                            <p class="card-text"><small class="text-muted">发证日期：{{ certificate.issue_date.strftime('%Y-%m-%d') }}</small></p>
                                            <p class="card-text"><small class="text-muted">证书编号：{{ certificate.number }}</small></p>
                                            <a href="{{ url_for('static', filename='uploads/' + certificate.file) }}" class="btn btn-sm btn-outline-primary" target="_blank">查看证书</a>
                                            <a href="{{ url_for('static', filename='uploads/' + certificate.file) }}" class="btn btn-sm btn-outline-secondary" download>下载证书</a>
                                        </div>
                                    </div>
                                </div>
                                {% endfor %}
                            </div>
                            {% else %}
                            <div class="text-center py-5">
                                <i class="fas fa-award fa-4x text-muted mb-3"></i>
                                <p class="lead">您还没有获得任何证书</p>
                                <a href="{{ url_for('main.courses') }}" class="btn btn-primary">参加认证课程</a>
                            </div>
                            {% endif %}
                        </div>
                    </div>
                </div>
                
                <div class="tab-pane fade" id="settings">
                    <div class="card shadow-sm">
                        <div class="card-header">
                            <h5 class="mb-0">账户设置</h5>
                        </div>
                        <div class="card-body">
                            <form method="post" action="{{ url_for('main.update_profile') }}">
                                {{ form.hidden_tag() }}
                                <div class="mb-3">
                                    {{ form.username.label(class="form-label") }}
                                    {{ form.username(class="form-control", value=current_user.username) }}
                                    {% for error in form.username.errors %}
                                    <div class="text-danger">{{ error }}</div>
                                    {% endfor %}
                                </div>
                                <div class="mb-3">
                                    {{ form.email.label(class="form-label") }}
                                    {{ form.email(class="form-control", value=current_user.email) }}
                                    {% for error in form.email.errors %}
                                    <div class="text-danger">{{ error }}</div>
                                    {% endfor %}
                                </div>
                                <div class="mb-3">
                                    {{ form.phone.label(class="form-label") }}
                                    {{ form.phone(class="form-control", value=current_user.phone) }}
                                    {% for error in form.phone.errors %}
                                    <div class="text-danger">{{ error }}</div>
                                    {% endfor %}
                                </div>
                                <div class="mb-3">
                                    {{ form.current_password.label(class="form-label") }}
                                    {{ form.current_password(class="form-control") }}
                                    {% for error in form.current_password.errors %}
                                    <div class="text-danger">{{ error }}</div>
                                    {% endfor %}
                                </div>
                                <div class="mb-3">
                                    {{ form.new_password.label(class="form-label") }}
                                    {{ form.new_password(class="form-control") }}
                                    {% for error in form.new_password.errors %}
                                    <div class="text-danger">{{ error }}</div>
                                    {% endfor %}
                                    <div class="form-text">如不修改密码，请留空</div>
                                </div>
                                <div class="mb-3">
                                    {{ form.confirm_password.label(class="form-label") }}
                                    {{ form.confirm_password(class="form-control") }}
                                    {% for error in form.confirm_password.errors %}
                                    <div class="text-danger">{{ error }}</div>
                                    {% endfor %}
                                </div>
                                <div class="d-grid gap-2">
                                    {{ form.submit(class="btn btn-primary") }}
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %} 